﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Bussiness;
using HTTT.DataAccess;
using HTTT.AD;
using HTTT.Utilities;
using System.Configuration;

namespace HTTT.admin.adgroup
{
    public partial class DeleteGroup : System.Web.UI.Page
    {
        /// <summary>
        ///  View State EditItemID
        /// </summary>
        private Guid ItemID
        {
            get
            {
                object obj = this.ViewState["_ItemID"];
                if (obj == null)
                    return Guid.NewGuid(); //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_ItemID"] = value;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }


        private void LoadData()
        {
            string TempID = Request.QueryString["ID"];
            Guid MyID;
            bool convert = Guid.TryParse(TempID, out MyID);
            HT_GroupsBO UserBO = new HT_GroupsBO();
            if (convert)
            {
                ItemID = MyID;
                string ouPath = LoadGroupDetail(ItemID);
                LoadOU(ouPath);
            }
            else
            {
                Response.Redirect("GroupList.aspx");
            }
        }
        private void LoadOU(string selectedValue)
        {
            //Get OU list
            HT_OUBO Bo = new HT_OUBO();
            List<HT_OU> ListBO = Bo.GetAllHT_OU().ToList();
            ddlOU.DataSource = ListBO;
            ddlOU.DataValueField = ADOUProperties.DISTINGUISHEDNAME;
            ddlOU.DataTextField = ADOUProperties.NAME;
            ddlOU.DataBind();
            ddlOU.Items.Insert(0, new ListItem("▼ " + ConfigurationManager.AppSettings["LDAPDomain"], ConfigurationManager.AppSettings["DomainPath"]));
            ddlOU.SelectedValue = selectedValue;
        }
        private string LoadGroupDetail(Guid id)
        {
            //Get UserInfo by username
            HT_GroupsDAO dao = new HT_GroupsDAO();
            HT_Groups Info = dao.GetHT_GroupsByID(id);
            txtName.Text = Info.name;
            txtDescription.Text = Info.description;
            rblGroupScope.SelectedValue = Info.groupScope.ToString();
            rblGroupType.SelectedValue = Convert.ToInt16(Info.isSecurityGroup).ToString();
            txtMember.Text = Info.members;
            string ouPath = Info.LDAPPath;
            ouPath = ouPath.Substring(ouPath.IndexOf(',') + 1);
            return ouPath;
        }


        private ReturnValue<bool> ValidateData()
        {
            if (txtFromRequest.Text.Trim() == string.Empty) return new ReturnValue<bool>(false, "Phải nhập thông tin yêu cầu");
            return new ReturnValue<bool>(true, "");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ReturnValue<bool> Valid = ValidateData();
            if (Valid.RetValue)
            {
                HT_GroupsBO bo = new HT_GroupsBO();
                bool result = bo.RequestDeleteGroup(ItemID, Page.User.Identity.Name, txtFromRequest.Text);
                if (result)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatMsgThenRedirect("Tạo yêu cầu xóa Group thành công !", "GroupList.aspx"), true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(Valid.Msg), true);
            }
        }
        protected void btnCancel_Click(Object sender, EventArgs e)
        {
            Response.Redirect("GroupList.aspx");
        }
    }
}